package com.mall.product.server.service.impl;

import java.util.List;
import com.mall.common.core.utils.DateUtils;
import com.mall.product.common.domain.PmsSkuInfo;
import com.mall.product.server.mapper.PmsSkuInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mall.product.server.mapper.PmsSpuInfoMapper;
import com.mall.product.common.domain.PmsSpuInfo;
import com.mall.product.server.service.IPmsSpuInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * spu信息Service业务层处理
 *
 * @author mall
 * @date 2024-12-18
 */
@Service
public class PmsSpuInfoServiceImpl implements IPmsSpuInfoService
{
    @Autowired
    private PmsSpuInfoMapper pmsSpuInfoMapper;
    @Autowired
    private PmsSkuInfoMapper pmsSkuInfoMapper;

    /**
     * 查询spu信息
     *
     * @param id spu信息主键
     * @return spu信息
     */
    @Override
    public PmsSpuInfo selectPmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoById(id);
    }

    /**
     * 查询spu信息列表
     *
     * @param pmsSpuInfo spu信息
     * @return spu信息
     */
    @Override
    public List<PmsSpuInfo> selectPmsSpuInfoList(PmsSpuInfo pmsSpuInfo)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoList(pmsSpuInfo);
    }

    /**
     * 新增spu信息
     *
     * @param pmsSpuInfo spu信息
     * @return 结果
     */
    @Override
    public int insertPmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setCreateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.insertPmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 修改spu信息
     *
     * @param pmsSpuInfo spu信息
     * @return 结果
     */
    @Override
    public int updatePmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setUpdateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.updatePmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 批量删除spu信息
     *
     * @param ids 需要删除的spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoByIds(Long[] ids)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoByIds(ids);
    }

    /**
     * 删除spu信息信息
     *
     * @param id spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoById(id);
    }

    @Transactional
    @Override
    public int updatePublish(Integer id) {
        //修改spu上下架状态
        //1.修改数据库spu表上下架状态
        //1.1先在数据库获取spu对象
        PmsSpuInfo pmsSpuInfo = pmsSpuInfoMapper.selectPmsSpuInfoById(Long.valueOf(id));
        Integer publishStatus = pmsSpuInfo.getPublishStatus();
        if(publishStatus==0){
            pmsSpuInfo.setPublishStatus(1);
        }else {
            pmsSpuInfo.setPublishStatus(0);
        }
        //1.2修改数据库spu表状态
        int i = pmsSpuInfoMapper.updatePmsSpuInfo(pmsSpuInfo);
        //2.根据spuID获取sku列表
        PmsSkuInfo pmsSkuInfo = new PmsSkuInfo();
        pmsSkuInfo.setSpuId(pmsSpuInfo.getId());
        List<PmsSkuInfo> pmsSkuInfos = pmsSkuInfoMapper.selectPmsSkuInfoList(pmsSkuInfo);
        //2.1数据建模
        //2.2调用esOpenFien 将sku列表对象添加进es




        return 0;
    }
}
